var express = require('express');
var router = express.Router();
var db = require("../sqlcon");
/* GET home page. */

router.get('/department_add', function(req, res, next) {
    res.render('department_add');
});
router.get('/department_edit', function(req, res, next) {
    res.render('department_edit');
});
router.get('/department_list', function(req, res, next) {
    var sql = "select * from department";
    db.query(sql,function(err,data){
        if(err){
            console.log(err);
        }
        else{
            res.render("department_list",{departList:data});
        }
    })
});

router.post("/adddepart",function(req,res,next){
    var departName = req.body.departname;
    var departTip = req.body.description;
    var departCode = req.body.departcode;
    var sql = "insert into department (departName,departTip,departCode) values (?,?,?)";
    db.query(sql,[departName,departTip,departCode],function(err,data){
        if(err){
            console.log(err)
        }
        else{
            //res.send("添加机构成功");
            res.render('department_add');
        }
    })
});
router.post("/deletedepart",function (req,res,next) {
    var departmentId = req.body.departmentId;
    var sql="delete from department where departmentId=?";
    db.query(sql,[departmentId],function (err, data) {
        if (err) {
            console.log(err);
        }
        else{
            res.send("删除成功");
        }
    })
})
router.post("/delet",function (req,res,next) {
    var choosestr = req.body.choose;
    var choosee=choosestr.split(",");
    var chooselist="?";
    for(var i=0;i<choosee.length-1;i++){
        chooselist +=",?";
    }
    var sql="delete from department where departmentId in ("+chooselist+")";
    db.query(sql,choosee,function (err, data) {
        if (err) {
            console.log(err);
        }
        else if (data.affectedRows==choosee.length) {
            res.send("删除成功");
        }
        else{
            res.send("删除异常");
        }
    })
})
router.post("/editdepart",function (req,res,next) {
    var departName = req.body.departname;
    var departTip = req.body.description;
    var departCode = req.body.departcode;
    var departmentId = req.body.departmentId;
    var sql="update department set departName=?,departTip=?,departCode=? where departmentId=?";
    db.query(sql,[departName,departTip,departCode,departmentId],function(err,data){
        console.log(data);
        if(err){
            console.log(err)
        }
        else if (data.affectedRows == 1){
            res.redirect("/department_list");
        }
        else{
            res.send("添加机构异常");
        }
    })
});
router.post("/loging",function(req,res,next){
    var userName = req.body.search;
    var sql = "select * from department where departName like '%"+userName+"%'";
    db.query(sql,function(err,data){
        if(err){
            res.send("数据库操作异常");
        }
        else if(data.length > 0){
            res.render("department_list",{departList:data});
        }
        else{
            res.send("机构名不正确");
        }
    })
})
/*
* 1、点击超链接，访问对应的路由处理
* 2、定义路由处理，router.get("/departlist",function(req,res){})
* 3、在回调函数中从数据库取数据，res.render(department_list,{departlist:departlist})
* 4、视图页面数据呈现
* */
module.exports = router;
